home *** CD-ROM | disk | FTP | other *** search
- /**********************************************
- HTBCALENDAR.DLL
- TransEra Corporation 1999.
- This is the source for the HTBCalendar Dll.
- Class derived from Shekar Narayanan
- ***********************************************/
-
- #include "stdafx.h"
- #include "YMSelector.h"
- #include <math.h>
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- static char *Months[] =
- {
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December"
- };
-
- static char *Weekdays[] =
- {
- "S", "M", "T", "W", "T", "F", "S"
- };
-
- static int DaysinMonth[] =
- {
- 31,28,31,30,31,30,31,31,30,31,30,31
- };
-
- /////////////////////////////////////////////////////////////////////////////
- // CYMPopUp
-
- CYMPopUp::CYMPopUp(CPoint p, CWnd* pParent, int nYear, int nMonth, int nDate)
- : m_bkBrush(::GetSysColor(COLOR_INFOBK))
- {
- if (-1 == nYear)
- m_nYear = CTime::GetCurrentTime().GetYear();
- else
- m_nYear = nYear;
-
- if (-1 == nMonth)
- m_nMonth = CTime::GetCurrentTime().GetMonth() -1;
- else
- m_nMonth = nMonth -1;
-
- if (-1 == nDate)
- m_nDate = CTime::GetCurrentTime().GetDay() -1;
- else
- m_nDate = nDate -1;
-
- ASSERT(m_nYear >= START_YEAR);
- ASSERT(m_nMonth >= 0 && m_nMonth <= 11);
- ASSERT(m_nDate >= 0 && m_nDate <= 30);
-
- // set number of rows and columns and margin
- nRows = 8;
- nColumns = 7;
- nMargins = 4;
- nStyle = TRADITIONAL_STYLE;
-
- Create(p, pParent);
- }
-
- CYMPopUp::~CYMPopUp()
- {
- m_bkBrush.DeleteObject();
- }
-
-
- BEGIN_MESSAGE_MAP(CYMPopUp, CWnd)
- //{{AFX_MSG_MAP(CYMPopUp)
- ON_WM_NCDESTROY()
- ON_WM_LBUTTONUP()
- ON_WM_PAINT()
- ON_WM_LBUTTONDOWN()
- ON_WM_KEYDOWN()
- ON_WM_RBUTTONUP()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CYMPopUp message handlers
-
-
- BOOL CYMPopUp::Create(CPoint p, CWnd* pParent)
- {
- m_pParent = pParent;
- ASSERT(m_pParent);
-
- CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS|CS_HREDRAW|CS_VREDRAW,
- 0, (HBRUSH)m_bkBrush,0);
-
- if (!CWnd::CreateEx(0, szClassName, _T(""), WS_VISIBLE|WS_POPUP|WS_BORDER,
- p.x, p.y, 250, 200,
- pParent->GetSafeHwnd(), 0, NULL))
- return FALSE;
-
- m_pParent->EnableWindow(false);
-
- UpdateWindow();
-
- return TRUE;
- }
-
- void CYMPopUp::OnNcDestroy()
- {
- CWnd::OnNcDestroy();
- delete this;
- }
-
- int CYMPopUp::GetDaysinMonth()
- {
- int nDays;
-
- nDays = DaysinMonth[m_nMonth];
- if (LeapYear() && m_nMonth == 1) nDays++;
- return (nDays);
- }
-
- void CYMPopUp::OnPaint()
- {
- CPaintDC dc(this);
- CString Buffer;
- CRect rClient;
- CRect rCell;
- CRect rStart;
- CFont fText;
- CFont* pfdc;
- short nIndex = 0;
-
- // get day associated with the first of the month
- int nFirstDayofMonth = FDOM ();
-
- // get number of days in the month;
- int nDays = GetDaysinMonth();
- int nCellsNeeded = nDays + nFirstDayofMonth + 1;
- //nRows = (nCellsNeeded - 1)/nColumns + 3;
-
- // get client dimensions
- GetClientRect(rClient);
-
- // compute cell height and width
- nCellHeight = (rClient.Height()-2*nMargins) / nRows;
- nCellWidth = (rClient.Width()-2*nMargins) / nColumns;
-
- // anchor point (top left)
- CPoint nAnchor;
- nAnchor.x = (rClient.Width() - nColumns*nCellWidth)/2;
- nAnchor.y = (rClient.Height() - nRows*nCellHeight)/2;
-
- // decrement button
- m_rMinus.left = nAnchor.x;
- m_rMinus.right = m_rMinus.left + nCellWidth;
- m_rMinus.top = nAnchor.y;
- m_rMinus.bottom = m_rMinus.top + nCellHeight;
-
- // increment button
- m_rPlus.right = rClient.right - nAnchor.x;
- m_rPlus.left = m_rPlus.right - nCellWidth;
- m_rPlus.top = nAnchor.y;
- m_rPlus.bottom = m_rPlus.top + nCellHeight;
-
- // placeholder for current month and year
- m_rYear.CopyRect(rClient);
- m_rYear.left = m_rMinus.right + 5;
- m_rYear.right = m_rPlus.left - 5;
- m_rYear.top = nAnchor.y;
- m_rYear.bottom = m_rYear.top + nCellHeight;
-
- fText.DeleteObject();
- fText.CreatePointFont(120,"Times New Roman");
- pfdc = dc.SelectObject(&fText);
-
- dc.SetBkColor(::GetSysColor(COLOR_INFOBK));
- dc.SetTextColor(::GetSysColor(COLOR_INFOTEXT));
-
- // show current month and year
- Buffer.Format("%s %d", Months[m_nMonth], m_nYear);
- dc.DrawText(Buffer, m_rYear, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
-
- // draw increment and decrement buttons
- rStart.CopyRect(m_rMinus); rStart.right++;
- dc.Rectangle (rStart);
- rStart.CopyRect(m_rPlus); rStart.right++;
- dc.Rectangle (rStart);
-
- dc.SetBkMode(TRANSPARENT);
- dc.DrawText("-", m_rMinus, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
- dc.DrawText("+", m_rPlus, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
- dc.SetBkMode(OPAQUE);
-
- dc.SelectObject(pfdc);
- fText.DeleteObject();
- fText.CreatePointFont(FONT_SIZE,"MS Sans Serif");
- pfdc = dc.SelectObject(&fText);
-
- // now draw the cells
- rCell.left = nAnchor.x;
- rCell.top = nAnchor.y + nCellHeight;
- rCell.right = rCell.left + nCellWidth;
- rCell.bottom = rCell.top + nCellHeight;
-
- // show the days of the week
- rStart.CopyRect(rCell);
- if (nStyle == TRADITIONAL_STYLE) {
- dc.MoveTo (m_rMinus.left, rStart.bottom);
- dc.LineTo (m_rPlus.right, rStart.bottom);
- }
-
- for (int j = 0; j < 7; j++) {
- if (nStyle == SIMPLE_STYLE) {
- dc.MoveTo(rCell.left,rCell.top);
- dc.LineTo(rCell.right,rCell.top);
- dc.LineTo(rCell.right,rCell.bottom);
-
- dc.MoveTo(rCell.left,rCell.top);
- dc.LineTo(rCell.left,rCell.bottom);
- }
- CRect rCurSel(rCell);
- rCurSel.DeflateRect(1,1);
- dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
- dc.SetTextColor(::GetSysColor(COLOR_INFOTEXT));
- dc.DrawText(Weekdays[j], rCell, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
- rCell.OffsetRect(nCellWidth,0);
- }
- rStart.OffsetRect(0,nCellHeight);
- rCell.CopyRect(rStart);
-
- BOOL bDone=FALSE;
-
- // show the dates (days of the month)
- for (int i = 1; i < nRows-1; i++)
- {
- for (int j = 0; j < nColumns; j++)
- {
- if (nStyle == SIMPLE_STYLE) {
- dc.MoveTo(rCell.left,rCell.top);
- dc.LineTo(rCell.right,rCell.top);
- dc.LineTo(rCell.right,rCell.bottom);
-
- dc.MoveTo(rCell.left,rCell.top);
- dc.LineTo(rCell.left,rCell.bottom);
- dc.LineTo(rCell.right,rCell.bottom);
- }
-
- CRect rCurSel(rCell);
- rCurSel.DeflateRect(1,1);
-
- // check whether the first of the month has been handled so far
- if (!bDone && ((j+1) == nFirstDayofMonth || nFirstDayofMonth == 0))
- {
- bDone = TRUE;
- dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
- rCell.OffsetRect(nCellWidth,0);
- }
- else if (bDone) {
- if (m_nDate == nIndex)
- {
- dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_HIGHLIGHT));
- dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
- }
- else
- {
- dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
- dc.SetTextColor(::GetSysColor(COLOR_INFOTEXT));
- }
-
- if (nIndex < nDays) {
- CString szT; szT.Format ("%2d",nIndex+1);
- dc.DrawText(szT, rCell, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
- m_rCells[nIndex].CopyRect(rCell);
- nIndex++;
- }
- rCell.OffsetRect(nCellWidth,0);
- }
- else {
- dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
- rCell.OffsetRect(nCellWidth,0);
- }
- }
-
- rStart.OffsetRect(0,nCellHeight);
- rCell.CopyRect(rStart);
- }
-
- dc.SelectObject(pfdc);
- fText.DeleteObject();
-
- }
-
- // -----------------------------------------------------------------------------
- void CYMPopUp::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CClientDC dc(this);
- CWnd::OnLButtonDown(nFlags, point);
-
- for (int i = 0; i < GetDaysinMonth(); i++)
- {
- if (m_rCells[i].PtInRect(point))
- {
- InvalidateRect(m_rCells[m_nDate]);
- m_nDate = i;
- InvalidateRect(m_rCells[i]);
- return;
- }
- }
-
- if (m_rMinus.PtInRect(point)) {
- dc.InvertRect(m_rMinus);
- }
-
- if (m_rPlus.PtInRect(point)) {
- dc.InvertRect(m_rPlus);
- }
-
- }
-
- void CYMPopUp::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CClientDC dc(this);
- CRect rClient;
-
- if (m_rMinus.PtInRect(point))
- {
- PreviousMonth ();
- dc.InvertRect(m_rMinus);
- Invalidate ();
- return;
- }
-
- if (m_rPlus.PtInRect(point))
- {
- NextMonth();
- dc.InvertRect(m_rPlus);
- Invalidate ();
- return;
- }
-
- if (ValidClick (point))
- {
- m_pParent->EnableWindow(true);
- int nComposite = m_nDate + 100*(m_nMonth+1);
- m_pParent->PostMessage(YM_SELECTED, (WPARAM)m_nYear, (LPARAM)nComposite);
- DestroyWindow();
- }
-
- }
-
- void CYMPopUp::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
- if (nChar == VK_ESCAPE)
- {
- m_pParent->EnableWindow(true);
- m_pParent->SendMessage(YM_ABORTED);
- DestroyWindow();
- }
- else if (nChar == VK_LEFT) {
- PreviousDay();
- Invalidate();
- }
- else if (nChar == VK_RIGHT) {
- NextDay();
- Invalidate();
- }
-
- }
- // -----------------------------------------------------------------------------
-
- void CYMPopUp::OnRButtonUp(UINT nFlags, CPoint point) // handles right mouse button clicks
- // in the + and - boxes
- {
- CClientDC dc(this);
-
- if (m_rMinus.PtInRect(point))
- {
- m_nYear--;
- dc.InvertRect(m_rMinus);
- Invalidate ();
- return;
- }
-
- if (m_rPlus.PtInRect(point))
- {
- m_nYear++;
- dc.InvertRect(m_rPlus);
- Invalidate ();
- return;
- }
-
- CWnd::OnRButtonUp(nFlags, point);
- }
-
- BOOL CYMPopUp::ValidClick (CPoint point) // checks whether mouse click is in one of the
- // cells containing a day of the current month
- {
- for (int i = 0; i < GetDaysinMonth(); i++)
- {
- if (m_rCells[i].PtInRect(point))
- return TRUE;
- }
- return FALSE;
- }
-
- void CYMPopUp::PreviousDay() // "sets" the previous day
- {
- m_nDate--;
- if (m_nDate < 0) {
- m_nMonth--;
- if (m_nMonth < 0) {
- m_nMonth = 11;
- m_nYear--;
- }
- m_nDate = GetDaysinMonth() - 1;
- }
- }
-
- void CYMPopUp::NextDay() // "sets" the next day
- {
- m_nDate++;
- if ((m_nDate+1) > GetDaysinMonth()) {
- m_nMonth++;
- if (m_nMonth > 11) {
- m_nMonth = 0;
- m_nYear++;
- }
- m_nDate = 0;
- }
- }
-
- void CYMPopUp::PreviousMonth() // "sets" the previous month
- {
- m_nMonth--;
- if (m_nMonth < 0) {
- m_nMonth = 11;
- m_nYear--;
- }
- if ((m_nDate+1) > GetDaysinMonth()) m_nDate=0;
- }
-
- void CYMPopUp::NextMonth() // "sets" the next month
- {
- m_nMonth++;
- if (m_nMonth > 11) {
- m_nMonth = 0;
- m_nYear++;
- }
- if ((m_nDate+1) > GetDaysinMonth()) m_nDate=0;
- }
-
- BOOL CYMPopUp::LeapYear () // is the current year a leap year?
- {
- if ((m_nYear % 4 == 0 && m_nYear % 100 != 0) || (m_nYear % 400 == 0))
- return (TRUE);
- else
- return (FALSE);
- }
-
- int CYMPopUp::FDOM () // obtains the day of week for the first of the current month
- {
- long jday = julday (m_nMonth+1, 1, m_nYear);
- int idwk = (int) ((jday+1) % 7);
-
- return (idwk);
- }
-
- #define IGREG (15+31L*(10+12L*1582)) // Gregorian Calendar (started in October 15, 1582)
- // procedure to compute day of week available
- // in Numerical Recipes in C, Press et. al.
- // Cambridge Press
-
- long CYMPopUp::julday (int mm, int id, int iyyy)
- {
- long jul;
- int ja, jy, jm;
-
- if (mm > 2) {
- jy = iyyy;
- jm = mm + 1;
- }
- else {
- jy = iyyy - 1;
- jm = mm + 13;
- }
- jul = (long) (floor(365.25*jy) + floor(30.6001*jm) + id + 1720995);
- if (id + 31L*(mm+12L*iyyy) >= IGREG) {
- ja = (int)(0.01f*jy);
- jul += 2-ja+(int)(0.25*ja);
- }
- return jul;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CYMSelector
-
- CYMSelector::CYMSelector()
- {
- m_nMonth = CTime::GetCurrentTime().GetMonth();
- m_nYear = CTime::GetCurrentTime().GetYear();
- m_nDate = CTime::GetCurrentTime().GetDay();
- }
-
- CYMSelector::~CYMSelector()
- {
- }
-
-
- BEGIN_MESSAGE_MAP(CYMSelector, CButton)
- //{{AFX_MSG_MAP(CYMSelector)
- ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
- //}}AFX_MSG_MAP
- ON_MESSAGE(YM_SELECTED, YMSelected)
- ON_MESSAGE(YM_ABORTED, YMAborted)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CYMSelector message handlers
-
- void CYMSelector::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- ASSERT(lpDrawItemStruct);
-
- CString BtnText;
- CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
- CRect rect = lpDrawItemStruct->rcItem;
- UINT state = lpDrawItemStruct->itemState;
-
- CRect rArrow(rect);
-
- rArrow.left = rArrow.right - 20;
-
- CSize Margins(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));
-
- pDC->DrawFrameControl(&rArrow, DFC_SCROLL, DFCS_SCROLLDOWN |
- ((state & ODS_SELECTED) ? DFCS_PUSHED : 0) |
- ((state & ODS_DISABLED) ? DFCS_INACTIVE : 0));
-
-
- rect.right -= rArrow.Width();
-
- pDC->FillSolidRect(rect,::GetSysColor(COLOR_WINDOW));
-
- GetWindowText(BtnText);
- if (state & ODS_DISABLED)
- pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
- else
- pDC->SetTextColor(::GetSysColor(COLOR_BTNTEXT));
-
- pDC->DrawText(BtnText, rect, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
-
- if (state & ODS_FOCUS)
- {
- rect.DeflateRect(1,1);
- pDC->DrawFocusRect(rect);
- }
-
- rect.InflateRect(1,1);
- pDC->DrawEdge(rect,EDGE_BUMP,BF_RECT);
-
-
- }
-
- void CYMSelector::PreSubclassWindow()
- {
- CButton::PreSubclassWindow();
- CString buffer;
- CTime tCurrent = CTime::GetCurrentTime();
- buffer = tCurrent.Format("%B %d, %Y");
- SetWindowText(buffer);
-
- m_nYear = tCurrent.GetYear();
- m_nMonth = tCurrent.GetMonth() - 1;
- m_nDate = tCurrent.GetDay() - 1;
- }
-
- void CYMSelector::OnClicked()
- {
- CRect rect;
- GetWindowRect(rect);
- rect.OffsetRect(0,rect.Height());
- GetParent()->EnableWindow(false);
-
- new CYMPopUp(CPoint(rect.left,rect.top), this,GetYear(),GetMonth(),GetDate());
-
- return;
-
- }
-
- LONG CYMSelector::YMSelected(WPARAM wParam , LPARAM lParam)
- {
- m_nYear = (int) wParam;
- m_nMonth = ((int) (lParam))/100 - 1;
- m_nDate = (int) lParam - 100*(m_nMonth+1);
-
- CString buffer;
- buffer.Format("%s %d, %d", GetMonthString(), GetDate(), GetYear());
- SetWindowText(buffer);
- GetParent()->EnableWindow(true);
- GetParent()->SetFocus();
-
- return 0;
- }
-
- LONG CYMSelector::YMAborted(WPARAM wParam , LPARAM lParam)
- {
- GetParent()->EnableWindow(true);
- GetParent()->SetFocus();
-
- return 0;
- }
-
- int CYMSelector::GetYear()
- {
- return m_nYear;
- }
-
- int CYMSelector::GetMonth()
- {
- return m_nMonth + 1;
- }
-
- int CYMSelector::GetDate()
- {
- return m_nDate + 1;
- }
-
- LPCTSTR CYMSelector::GetMonthString()
- {
- return Months[m_nMonth];
- }
-
- void CYMSelector::SetMonth(int m)
- {
- m--;
- ASSERT(m >= 0 && m <= 11);
- m_nMonth = m;
-
- CString buffer;
- buffer.Format("%s, %d", GetMonthString(), GetYear());
- SetWindowText(buffer);
- }
-
-
- void CYMSelector::SetDate(int d)
- {
- d--;
- ASSERT(d < GetDaysinMonth());
- m_nDate = d;
-
- CString buffer;
- buffer.Format("%s %d, %d", GetMonthString(), GetDate(), GetYear());
- SetWindowText(buffer);
- }
-
-
- void CYMSelector::SetYear(int y)
- {
-
- ASSERT(y >= START_YEAR);
- m_nYear = y;
-
- CString buffer;
- buffer.Format("%s %d, %d", GetMonthString(), GetDate(), GetYear());
- SetWindowText(buffer);
- }
-
- int CYMSelector::GetDaysinMonth()
- {
- int nDays;
-
- nDays = DaysinMonth[m_nMonth];
- if (LeapYear() && m_nMonth == 1) nDays++;
- return (nDays);
- }
-
- BOOL CYMSelector::LeapYear () // is the current year a leap year?
- {
- if ((m_nYear % 4 == 0 && m_nYear % 100 != 0) || (m_nYear % 400 == 0))
- return (TRUE);
- else
- return (FALSE);
- }
-
-